package com.foyekoo.project001.utils;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.foyekoo.project001.model.Bill;

import java.util.ArrayList;
import java.util.List;

public class BillUtil {

    public static void init() {
        DBUtil.initBillDB();
    }


    public static List<Bill> getAllBill() {
        List<Bill> list = new ArrayList<>();
        SQLiteDatabase db = DBUtil.initBillDB();
        Cursor cursor = db.rawQuery("select * from bill",null);

        LogUtil.d("BillUtil","-->" + cursor.moveToNext() + "," + cursor.getCount());
        cursor.moveToFirst();
        if(cursor.getCount() > 0) {
            do {
                Bill bill = new Bill();
                String date = cursor.getString(cursor.getColumnIndex("bill_date"));
                String coinNum = cursor.getString(cursor.getColumnIndex("coin_num"));
                String prizeNum = cursor.getString(cursor.getColumnIndex("prize_num"));

                LogUtil.d("BillUtil", "selector:" + date + "," + coinNum + "," + prizeNum);

                bill.date = date;
                bill.coinNum = Integer.parseInt(coinNum);
                bill.prizeNum = Integer.parseInt(prizeNum);
                list.add(bill);
            } while (cursor.moveToNext());
        }
        db.close();
        return list;

    }

    public static void  insert(String date, String coin, String prize) {
        SQLiteDatabase db = DBUtil.initBillDB();
        Cursor cursor = db.rawQuery("select * from bill where bill_date=?", new String[]{date});
        if (cursor.getCount() > 0) { //证明当天的数据是有的
            cursor.moveToFirst();
            int coinNum = Integer.parseInt(coin);
            int prizeNum = Integer.parseInt(prize);
            coinNum += Integer.parseInt(cursor.getString(cursor.getColumnIndex("coin_num")));
            prizeNum += Integer.parseInt(cursor.getString(cursor.getColumnIndex("prize_num")));


            LogUtil.d("BillUtil","--->" +coinNum + "," + prizeNum);

            //把旧的叠加进去

            ContentValues contentValues = new ContentValues();
            contentValues.put("coin_num", (coinNum + ""));
            contentValues.put("prize_num", (prizeNum + ""));

            db.update("bill",contentValues,"bill_date = ?" ,new String[]{date}); //更新到库里面
            LogUtil.d("BillUtil","update");
        } else {
            ContentValues contentValues = new ContentValues();
            contentValues.put("bill_date", date);
            contentValues.put("coin_num", coin);
            contentValues.put("prize_num", prize);
            db.insert("bill", null, contentValues); //插入一条新的;
            LogUtil.d("BillUtil","insert");
        }
        db.close();
    }

    public static void updateAllCoin(String coin) {

    }

    /**
     * 清空当天的的记录
     * @param date
     */
    public static void updateCurDay(String date) {
        SQLiteDatabase db = DBUtil.initBillDB();
        Cursor cursor = db.rawQuery("select * from bill where bill_date=?", new String[]{date});
        if (cursor.getCount() > 0){
            ContentValues contentValues = new ContentValues();
            contentValues.put("coin_num", (0 + ""));
            contentValues.put("prize_num", (0 + ""));

            db.update("bill",contentValues,"bill_date = ?" ,new String[]{date}); //更新到库里面
        }
    }
}
